import axios from "axios";
import { showMessage } from "./status";
import i18n from "@/lang";

// 设置接口超时时间
axios.defaults.timeout = 60000;
axios.defaults.baseURL = import.meta.env.VITE_API_DOMAIN;

console.log(import.meta.env)

var network_error = 0;
console.log('::: ', );
axios.interceptors.request.use(
	(config : any) => {
		// 配置请求头
		config.headers = {
			"Content-Type": "application/json;charset=UTF-8",
			//"Authorization":"zijin"
		}
		
		config.headers["Authorization"] =  import.meta.env.DEV ? "zijin":'';
		
		if (config.method == 'post') {
			config.headers["Content-Type"] = 'multipart/form-data'
		}
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);
//http request 拦截器
axios.interceptors.response.use(
	(response) => {	
	
		if (response.data.result && response.data.result.code !== 200) {
			loadStr(response);
			
			if (response.data.result.code==401){
				if (network_error == 0) {
					network_error = 1;
					//ElMessage.warning(response.data.result.message);
					setTimeout(function () {
						network_error = 0;
					}, 3000);
				}
				
				response.data.result.data={
					data:[],
					all_ids: [],
				}
				
				if (response.headers['content-disposition']) {
					response.data.contentDisposition = response.headers['content-disposition'];
				}
				return response;
			}if (response.data.result.code==403){
				let local=localStorage.getItem("old_status");
				if (!local || local!=window.location.hash){
					if (network_error == 0) {
						network_error = 1;
						ElMessage.error(response.data.result.message);
						
						setTimeout(function () {
							network_error = 0;
						}, 3000);
					}
					localStorage.setItem("old_status", window.location.hash);
				}
			}else{
				if (response.data.result.code==50111 || response.data.result.code==50112){
					return response;
				}
				if (response.data.result.code!=50110 && response.data.result.code!=400){
					if (network_error == 0){
						network_error=1;
						ElMessage.error(response.data.result.message);
						
						setTimeout(function () {
							network_error = 0;
						}, 3000);
					}
				}else if (response.data.result.code==50110){	
					let url=response.request.responseURL;
					if (url.indexOf("quotation/detail")>0 || url.indexOf("wx/unfollow")>0 || url.indexOf("my/info/edit")>0){
						return response
					}else if (network_error==0){		
						network_error=1;	
						if (response.data.result.message.indexOf("problem_image")==-1){
							let msg=i18n.global.t("common.pageNotFound");
							response.data.result.message=msg;
						}
						ElMessage.error(response.data.result.message);
						setTimeout(function () {
							network_error = 0;
						}, 3000);
					}
					
				}else if (network_error==0){
					network_error=1;
					ElMessage.error(response.data.result.message);
					
					setTimeout(function () {
						network_error = 0;
					}, 3000);
				}
			}
			return Promise.reject(response);
		}
		if (response.headers['content-disposition']) {
			response.data.contentDisposition = response.headers['content-disposition'];
		}
		
		return response;
	},
	(error) => {
		const { response } = error;

		if (response) {
			//showMessage(response.status);
	 		if (network_error == 0) {
				let msg = showMessage(response.status);
				if (!msg){
					msg="网络连接异常,请稍后再试!";
				}
				network_error = 1;
				ElMessage.warning(msg);
				setTimeout(function () {
					network_error = 0;
				}, 3000);
			}
		} else {
			if (network_error == 0) {
				network_error = 1;
				ElMessage.warning("网络连接异常,请稍后再试!");
				setTimeout(function () {
					network_error = 0;
				}, 3000);
			}
		}
		return Promise.reject(response);
	}
);

function loadStr(response){
	if (response.data.result.message.indexOf("problem_image")>=0){
		
	}else if (response.data.result.code==50114){
		response.data.result.message=i18n.global.t("accounts.insufficientUsers");
	}else if (response.data.result.code==50110){
		response.data.result.message=i18n.global.t("my.safetyInputTip");
	}else if (response.data.result.code==50112){
		response.data.result.message=i18n.global.t("my.correctPassword");
	}
	
	//50110 ，用户没有填写密码
	
	//50112，用户密码填写错误
	

}


// get 请求应 json post 请求用 FromData 格式
export function request(url = "", params = {}, type = "GET") {
	return new Promise((resolve, reject) => {
		let promise : any = "";
		let timeout=axios.defaults.timeout;
		if (params.timeout){
			timeout=params.timeout;
			delete params.timeout;
		}
		
		if (type.toUpperCase() === "GET") {
			const headers = { "Content-Type": "application/json;charset=UTF-8"};
			promise = axios({
				headers,
				url,
				params,
				timeout
			});
		} else if (type.toUpperCase() === "POST") {
			const headers = { "Content-Type": "multipart/form-data" };
			promise = axios({
				headers,
				method: "POST",
				url,
				data: params,
				timeout
			});
		}
		//处理返回
		promise
			.then((res : any) => {
				resolve(res);
			})
			.catch((err : any) => {
				reject(err);
			});
	});
}

// post 请求用 FromData 格式
export function httpDownload(url = "", params = {}, type = "GET") {
	return new Promise((resolve, reject) => {
		let promise : any = "";
		if (type.toUpperCase() === "GET") {
			const headers = { "Content-Type": "application/json;charset=UTF-8" };
			promise = axios({
				headers,
				url,
				params,
				responseType:"arraybuffer"
			});
		} else if (type.toUpperCase() === "POST") {
			const headers = { "Content-Type": "multipart/form-data" };
			promise = axios({
				headers,
				method: "POST",
				url,
				data: params,
			});
		}

		//处理返回
		promise
			.then((res : any) => {
				resolve(res);
			})
			.catch((err : any) => {
				reject(err);
			});
	});
}